/**
 * 
 */
package org.swing.utility.input.action;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Action;
import org.swing.utility.input.trigger.Trigger;

/**
 * @author  Le Quynh Nhu
 *
 */
public class ActionTrigger implements Trigger {
    private Action triggered;
    private List<String> triggers = new ArrayList<String>();
    private List<String> armed = new ArrayList<String>();

    public ActionTrigger(Action triggered) {
        this.triggered = triggered;
    }

    public void arm(ActionEvent e) {
        String command = e.getActionCommand();
        if (!triggers.remove(command)) return;
        armed.add(command);
        if (triggers.isEmpty()) {
            triggered.actionPerformed(e);
        }
    }

    public void disarm(ActionEvent e) {
        String command = e.getActionCommand();
        if (!armed.remove(command)) return;
        triggers.add(command);
    }

    public void addTrigger(String trigger) {
        triggers.add(trigger);
    }
}
